package com.hzw.saas.api.hpc.bo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.hzw.saas.api.hpc.enums.HpcTaskStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 用户机时明细
 * </p>
 *
 * @author zzl
 * @since 2021-02-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "用户机时明细")
public class HpcAccountDetailBo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 详情ID
     */
    @ApiModelProperty(value = "详情ID", position = 1)
    private String detailId;

    /**
     * 用户的HPC账号ID
     */
    @ApiModelProperty(value = "用户的HPC-RECORD-ID", position = 2)
    private String hpcAccountRecordId;

    /**
     * 超算账户名称
     */
    @ApiModelProperty(value = "超算账户名称", position = 3)
    private String hpcAccount;

    /**
     * 任务ID
     */
    @ApiModelProperty(value = "任务ID", position = 4)
    @Excel(name = "任务编号", width = 30, orderNum = "1")
    private String taskId;

    /**
     * 任务名称
     */
    @ApiModelProperty(value = "任务名称", position = 5)
    @Excel(name = "任务名称", width = 30, orderNum = "2")
    private String taskName;

    /**
     * 使用软件
     */
    @ApiModelProperty(value = "使用软件", position = 6)
    private String software;

    /**
     * 任务类型
     */
    @ApiModelProperty(value = "任务类型", position = 7)
    private String taskType;

    /**
     * 任务输入
     */
    @ApiModelProperty(value = "任务输入", position = 8)
    private String taskInput;

    /**
     * 任务结果
     */
    @ApiModelProperty(value = "任务结果", position = 9)
    @Excel(name = "计算状态", width = 30, orderNum = "5")
    private String taskResult;

    /**
     * 任务状态
     */
    @ApiModelProperty(value = "任务状态", position = 10)
    private Integer taskStatus;

    /**
     * 任务状态文本
     */
    @ApiModelProperty(value = "任务状态文本", position = 10)
    private String taskStatusText;

    /**
     * 超算运行时长，毫秒
     */
    @ApiModelProperty(value = "超算运行时长，毫秒", position = 11)
    @Excel(name = "运行时长（毫秒）", width = 30, orderNum = "7")
    private Long hpcTime;

    /**
     * 运行时长格式化文本，HH:mm:ss
     */
    @ApiModelProperty(value = "运行时长格式化文本，HH:mm:ss", position = 12)
    private String hpcTimeFormat;

    /**
     * 超算核数
     */
    @ApiModelProperty(value = "超算核数", position = 13)
    @Excel(name = "核数", width = 30, orderNum = "6")
    private Double hpcCore;

    /**
     * 消费机时
     */
    @ApiModelProperty(value = "消费机时", position = 14)
    @Excel(name = "消耗算力（核时）", width = 30, orderNum = "8")
    private Double hpcCoreTime;

    /**
     * 开始时间
     */
    @ApiModelProperty(value = "开始时间", position = 15)
    @Excel(name = "开始时间", width = 30, orderNum = "3")
    private Date startTime;

    /**
     * 结束时间
     */
    @ApiModelProperty(value = "结束时间", position = 16)
    @Excel(name = "结束时间", width = 30, orderNum = "4")
    private Date endTime;

    /**
     * 记录时间
     */
    @ApiModelProperty(value = "记录时间", position = 17)
    private Date recordTime;

    public void onReady() {
        this.setTaskStatusText(HpcTaskStatus.text(this.getTaskStatus()));
        this.setHpcTimeFormat(formatMilliseconds2Hour(this.getHpcTime()));
        // 2021-04-12 去除(去并行科技查询的数据就是使用的核时,不需要再次进行计算)
//        this.setHpcCoreTime(computeCoreTime(this.getHpcCore(), this.getHpcTime()));
    }

    public static double computeCoreTime(Double core, Long time) {
        double coreValue = core == null ? 0 : core;
        double timeValue = time == null ? 0 : NumberUtil.div(time.doubleValue(), (double) 1000 * 3600);
        return coreValue * timeValue;
    }

    public static String formatMilliseconds2Hour(long timeValue) {
        Date date = new Date(timeValue);
        Date startDate = new Date(0);
        long hours = DateUtil.between(startDate, date, DateUnit.HOUR);
        String[] format = DateUtil.format(date, "HH:mm:ss").split(":");
        return String.format("%s:%s:%s", hours, format[1], format[2]);
    }

}
